EasyUDPSocket Class

Supports communication via the UDP protocol using unicasting, multicasting, or broadcasting. It provides a simple 'wrapper' around the UDPSocket class that makes easier for beginners to get started with UDP networking.

Events

Error

ReceivedMessage


Properties

None

Methods

Bind

Register

SendMessageToGroup

SendMessageToIndividual

Unregister


More information available in parent classes: UDPSocket:SocketCore:Object

The DataAvailable event of the UDPSocket class is not available.

Even though you have access to the Write and Read methods of the UDPSocket class, you should never call them. Doing so will cause a RuntimeException to be raised (with an appropriate message set). This is so the internal protocol is enforced.


Notes

The EasyUDPSocket class provides a simple 'wrapper' around the UDPSocket class that makes easier for beginners to get started with UDP networking. With the EasyUDPSocket class, you can send messages either to an individual or to an entire group. One main benefit to the EasyUDPSocket class is the way it handles multicasting. It's a nuisance to look up which IP addresses can be used with multicasting.

The Command parameter can be used as a code to identify the type of data that is sent. For example, you could send a command ID of 100 to mean that the data is actually a memory block containing a FolderItem. Or, you could define ID 101 as the username of a remote application. This message mode is enforced on you in that you cannot use an arbitrary Write command. If you'd like to send arbitrary data, then you can just make up a miscellaneous command ID and send your arbitrary data.

Command ID's less than 0 are reserved for internal use. When you are sending messages, you should not use a command ID less than 0, as it may very well cause issues with other classes.

To send a message to a group, you pass the group name that you have previously assigned to the group using the Register method. This means that you can pass "MyLocalGroup" as the parameter to the Register method and use that string for calls to SendMessageToGroup. To send a message to an individual, you need the IP address of the user's computer in the SendMessageToIndividual.

You can still pass a valid Class D IP address; REALbasic will use it. One other helper function is the Bind function. This sets up the socket for your properly (so you don't have to set RouterHops or SendToSelf up yourself) and does the bind on the port specified.

One thing to keep in mind is that EasyUDPSocket defaults to having SendToSelf on. You are welcome to override this default yourself by setting SendToSelf to False after the making the Bind call. While you can still call [Join/Leave]MulticastGroup, we suggest that you only use Register and Unregister for your EasyUDPSocket application.


Examples

Binding to a port. EditField1 contains the user-specified port number:

EasyUDPSocket1.Bind( Val(EditField1.Text)))

Registering in a group. Everyone on the network should use the same name.

EasyUDPSocket1.Register("MyGroup")

Sending a message to the group. EditField1 is the text of the message.

EasyUDPSocket1.SendMessageToGroup("MyGroup",100,EditField1.Text)

Sending a message to an individual in the group. The individual's IP address and port is passed as the first parameter. The second parameter is the value of the Command parameter and, EditField1 contains the text of the message.

Dim port as String
port = Str(EasyUDPSocket1.Port)
EasyUDPSocket2.SendMessageToIndividual("192.168.1.152" +":"+ port _
            ,100, EditField1.Text )

Receiving a message in the ReceivedMessage event:

Sub ReceivedMessage (fromIP as String, Command as Integer, Data as String)
  MsgBox "Received " + Str(command) + ": " + data + " from " + fromIP _
          + EndOfLine

See Also

AutoDiscovery, SocketCore, UDPSocket classes.